package com.bestvike.stone.spring.controller;

import com.bestvike.stone.spring.context.ThreadContext;
import com.bestvike.stone.spring.exception.BusinessException;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.auth.Auth;
import com.bestvike.stone.spring.shiro.authz.exception.AuthorizationException;
import com.bestvike.stone.spring.trace.rest.ErrorHandler;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;

/**
 * Created by 许崇雷 on 2018-08-22.
 */
public class BaseController {
    protected final Log log = LogFactory.getLog(this.getClass());

    public static <TBody> CommonResponse<TBody> success() {
        return CommonResponse.success();
    }

    public static <TBody> CommonResponse<TBody> success(TBody body) {
        return CommonResponse.success(body);
    }

    protected final String getToken() {
        return ThreadContext.getToken();
    }

    protected final <TAuth extends Auth> TAuth getAuth() {
        return ThreadContext.getAuth();
    }

    protected final <TUserData> TUserData getUserData() {
        return ThreadContext.getUserData();
    }

    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public final ResponseEntity<CommonResponse> handleBusinessException(BusinessException e) {
        return ErrorHandler.handleBusinessException(e);
    }

    @ResponseBody
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public final ResponseEntity<CommonResponse> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        return ErrorHandler.handleMissingServletRequestParameterException(e);
    }

    @ResponseBody
    @ExceptionHandler(ResponseStatusException.class)
    public final ResponseEntity<CommonResponse> handleResponseStatusException(ResponseStatusException e) {
        return ErrorHandler.handleResponseStatusException(e);
    }

    @ResponseBody
    @ExceptionHandler(AuthorizationException.class)
    public final ResponseEntity<CommonResponse> handleAuthorizationExceptionException(AuthorizationException e) {
        return ErrorHandler.handleAuthorizationExceptionException(e);
    }

    @ResponseBody
    @ExceptionHandler(ClientAbortException.class)
    public final ResponseEntity<CommonResponse> handleClientAbortException(ClientAbortException e) throws ClientAbortException {
        return ErrorHandler.handleClientAbortException(e);
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public final ResponseEntity<CommonResponse> handleException(Exception e) {
        return ErrorHandler.handleException(e);
    }
}
